package com.dsx.wechat.web;

import com.dsx.common.util.SignUtil;
import com.dsx.wechat.service.MessageService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;

/**
 * Created by tianwenqing on 2017/9/6.
 */
@RestController
public class ConfirmWechatController {

    @GetMapping("/index")
    public String confirmWechat(@RequestParam(required = false) String signature ,@RequestParam(required = false) String timestamp
            ,@RequestParam(required = false) String nonce ,@RequestParam(required = false) String echostr){
        if(SignUtil.checkSignature(signature,timestamp,nonce)){
            return echostr;
        }
        return null ;
    }

    @PostMapping("/index")
    public String dealMessage(HttpServletRequest request , HttpServletResponse response){
        // 消息的接收、处理、响应
        // 将请求、响应的编码均设置为UTF-8（防止中文乱码）
        return  MessageService.processRequest(request);
       /* try {
          *//*  request.setCharacterEncoding("UTF-8");
            response.setCharacterEncoding("UTF-8");*//*
            return  MessageService.processRequest(request);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }*/

//        return  null ;
    }

    @GetMapping("/index1")
    public String confirmWechat1(){
        return "wenq" ;
    }

    @PostMapping("index1")
    public String comWechat(){
        return "你好";
    }
}
